package de.negativrekord.blockworld;

import cc.creativecomputing.math.CCMath;
import cc.creativecomputing.math.CCVector3f;

public class CameraPath {

	public CameraPath() {
		jump(currentIndex);
	}
	
	
	CCVector3f currentPosition = new CCVector3f();
	CCVector3f dir = new CCVector3f();
	int currentIndex = 0;
	int nextIndex = 1;
	
	int res = 100;
	
	float changeAngle = 0;
	
	CCVector3f points[] = {
			
			new CCVector3f(1000, 2300, 300),
			new CCVector3f(1000, 2300, -2200),
			new CCVector3f(1000, -300, -2200),
			new CCVector3f(1000, -300, 300)
			
			
//			new CCVector3f(0,700,150),
//			new CCVector3f(0,700,-600),
	};
	
	float tiltUD[] = {
			CCMath.PI*0.5f,
			CCMath.PI*0.5f,
			CCMath.PI*0.5f,
			CCMath.PI*0.5f,
	};
			
	float tiltLR[] = {
			CCMath.PI*0,
			CCMath.PI,
			CCMath.PI*2,
			CCMath.PI*3,
			
	};
	
	float pan[] = {
			CCMath.PI*0,
			CCMath.PI*0.5f,
			CCMath.PI*1f,
			CCMath.PI*0.5f,
	};

	float currentTiltUD = tiltUD[0];
	float currentTiltLR = tiltLR[0];
	float currentPan = pan[0];
	
	public void reset() {
		
	}
	
	public void jump (int thePoint) {
		if (thePoint<points.length) {
			currentPosition = points[thePoint];
		}
	}
	
	public CCVector3f direction () {
		return new CCVector3f(dir).normalize();
	}
	
	public CCVector3f currentPos() {
		return currentPosition;
	}
	
	public float tiltLR() {
		return currentTiltLR;
	}
	
	public float tiltUD() {
		return currentTiltUD;
	}
	
	
	public float pan() {
		return currentPan;
	}
	
	
	int cnt = 0;
	public void move (float theSpeed) {
		
		// calc dir increment
		dir = new CCVector3f(points[nextIndex]);
		dir.subtract(points[currentIndex]);
		dir.scale(1f/res);
		
		// calc angle increment
		float dTiltLR = (tiltLR[nextIndex] - (tiltLR[currentIndex] % CCMath.TWO_PI)) / res;
		float dTiltUD = (tiltUD[nextIndex] - (tiltUD[currentIndex] % CCMath.TWO_PI)) / res;
		
		float dPan  = (pan[nextIndex] - pan[currentIndex]) / res;
		
		if (cnt == res) {
			cnt = 0;
			currentIndex = (currentIndex+1) % points.length;
			nextIndex = (nextIndex+1) % points.length;
			
		}
		
		
		// move 
		currentPosition = new CCVector3f(points[currentIndex]).add(new CCVector3f(dir).scale(cnt));
		cnt += 1;
		
		currentTiltLR = tiltLR[currentIndex] + dTiltLR*cnt;
		currentTiltUD = tiltUD[currentIndex] + dTiltUD*cnt;
		currentPan  = pan[currentIndex] + dPan*cnt;
		/*
		if (currentIndex>0 && changeAngle<1) {
			currentTiltAngle = CCMath.blend(tilt[currentIndex-1], tilt[currentIndex], changeAngle);
			currentPan = CCMath.blend(pan[currentIndex-1], pan[currentIndex], changeAngle);
			changeAngle+=0.01f;
			CCLog.info(changeAngle);
		}
		
		
		float dist = currentPosition.distance(points[nextIndex]);
		if (dist<10) {
			currentIndex = (currentIndex+1) % points.length;
			nextIndex = (currentIndex+1) % points.length;
			changeAngle = 0;
		}
		*/
	}
}
